<?php
/**
 * 后台相关
 * @author fanzhanao
 *
 */
class cartoon_controller_admin_adv extends AdminController{
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->data = Model::factory('cartoon_model_adv');
	}
	
	/**
	 * 添加修改
	 *
	 */
	public function add_action(){
		$args['adv_title']	=	_('title');
		$args['adv_desc'] = _('info');
		$args['adv_flag']	=	1;
		$args['adv_order']	=	_('order',0);
		$args['adv_exprie'] = _('exprie',0);
		$args['adv_link']	=	_('link');
		$args['adv_time']	=	time();
		$args['adv_position'] = _('pos',1);
		if($_FILES['logo'] && !$_FILES['logo']['error']){
			$path = 'advimg/';
			$filename = md5(CURRENT_TIME.rand(1,10000));
			$rs = cartoon_util_upload::upload_file('logo',$path,substr($filename,0,rand(10,strlen($filenames)-10)));
			if($rs['result']){
				$args['adv_logo'] = UPLOAD_DOMAIN.'/'.$rs['url'];
			}
		}

		$this->data->add($args);
		
		cartoon_util_interface::cacheGetAdv($args['adv_position'],true);
		
		self::_redirect($this->app,$this->controller,'list');
		/**
		 * 是否為修改
		 */
		/*if($args['id']){
			if($this->data->edit($args)){
				$this->ret = self::success();
			}else $this->ret = self::failure('修改失败');
		}else{
			if($this->data->add($args)){
				$this->ret = self::success();
			}
			else{
				$this->ret = self::failure('添加失败');
			}
		}*/
	}
	
	/**
	 * 删除
	 *
	 */
	public function del_action(){
		$id = _('id');
		$this->_renderJson();
		if($this->data->remove($id)){
			/**
			 * 更新缓存
			 */
			cartoon_util_interface::cacheGetAdv(1,true);
			cartoon_util_interface::cacheGetAdv(2,true);
			$this->ret = self::success();
		}else $this->ret = self::failure('删除失败');
	}
	
	public function set_flag_action(){
		$id = _('id');
		$flag = _('flag');
		$this->_renderJson();
		if($this->data->setFlag($id,$flag)){
			cartoon_util_interface::cacheGetAdv($flag,true);
			$this->ret = self::success();
		}else $this->ret = self::failure('设置失败');
	}
	
	public function list_action(){
		$page = _('page',1);
		
		$args['pos'] = _('pos');
		
		$list = $this->data->getListPages($args,$page,30);
		
		$pages = $this->genPages($list['INFO']);
		
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$pages);
		
	}
}